import 'package:fluttertoast/fluttertoast.dart';
import 'package:pixelyear/bean/ColorBean.dart';
import 'package:pixelyear/sql/Sql.dart';
import 'package:pixelyear/utils/constant/YxsColor.dart';
import 'package:sqflite/sqflite.dart';

class ColorSqlUtils extends Sql {
  ColorSqlUtils._privateConstructor();

  static final ColorSqlUtils _instance = ColorSqlUtils._privateConstructor();

  static ColorSqlUtils get instance => _instance;

  Future<ColorBean> addColor(
      int id, int color, String text, int position) async {
    bool exists = await isExists(id);
    Database db = await getDb();
    if (!exists) {
      await db.rawInsert("insert into " +
          Sql.colorName +
          " (color,msg,position) values('${color.toString()}','$text','${position.toString()}')");
      await close();
      return getFirstData();
    } else {
      await db.rawUpdate(
          "update " +
              Sql.colorName +
              " set color=? , msg=?,position=? where _id=?",
          [color.toString(), text, position.toString(), id.toString()]);
      await close();
      return null;
    }
  }

  void addDefault() async {
    bool empty = await dataIsNull();
    if (empty) {
      await addColor(-1, YxsColor.color_one, "愿你多吃不胖", 0);
      await addColor(-1, YxsColor.color_two, "愿你深情不负", 1);
      await addColor(-1, YxsColor.color_three, "愿你傻人傻福", 2);
      await addColor(-1, YxsColor.color_four, "想要的都拥有", 3);
      await addColor(-1, YxsColor.color_five, "得不到的都释怀", 4);
    }
  }

  Future<List<ColorBean>> getData() async {
    Database db = await getDb();
    List<Map<String, dynamic>> cursor = await db
        .rawQuery("select * from " + Sql.colorName + " order by position asc");
    List<ColorBean> list = [];
    for (Map<String, dynamic> map in cursor) {
      var id = map["_id"];
      var color = map["color"];
      var msg = map["msg"];
      var bean = ColorBean(color, msg);
      bean.id = id;

      list.add(bean);
    }
    await close();
    return list;
  }

  void delete() async {
    Database db = await getDb();
    await db.rawDelete("delete from " + Sql.colorName);
    await close();
  }

  Future<bool> isExists(int id) async {
    if (id == -1) return false;
    Database db = await getDb();
    List<Map<String, dynamic>> cursor = await db.rawQuery(
        "select * from " + Sql.colorName + " where id = ?", [id.toString()]);
    await close();
    return cursor.isNotEmpty;
  }

  Future<bool> dataIsNull() async {
    Database db = await getDb();
    List<Map<String, dynamic>> cursor =
        await db.rawQuery("select * from " + Sql.colorName + "", []);
    await close();
    return cursor.isEmpty;
  }

  Future<ColorBean> getFirstData() async {
    Database db = await getDb();
    List<Map<String, dynamic>> cursor =
        await db.rawQuery("select * from " + Sql.colorName + "", []);
    await close();
    bool moveToFirst = cursor.isNotEmpty;
    if (moveToFirst) {
      var id = cursor[0]["_id"];
      var color = cursor[0]["color"];
      var msg = cursor[0]["msg"];
      ColorBean bean = ColorBean(color, msg);
      bean.id = id;
      return bean;
    }
    return null;
  }
}
